/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.modules.job.task;

import io.renren.modules.job.utils.MailAttachment;
import io.renren.modules.job.utils.MailUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.mail.Message;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 心跳定时任务
 * <p>
 * testTask为spring bean的名称
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component("mainTask")
public class MainTask implements ITask {
    private Logger logger = LoggerFactory.getLogger(getClass());

    public MainTask() {
        System.out.println("============执行了");
    }

    public static int isInit = 0;

    @Override
    public void run(String params) {
        logger.error("heartTask定时任务正在执行，参数为：{}", params);
        if (isInit==0){
            System.out.println("=====初始执行");
            isInit=1;
        }
        try {
            Runtime.getRuntime().exec("");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println("================1111");
        long startTime = System.currentTimeMillis();
        MailUtil mailUtil = new MailUtil();
        Message[] messages = new Message[0];
        try {
            messages = mailUtil.getPopMessages();
            System.out.println("==========");
            for (Message message : messages) {
                String subject = mailUtil.getSubject(message);
                System.out.println("打开邮件=====" + subject);
                List<MailAttachment> mailAttachments = new ArrayList<>();
                mailUtil.getAttachment(message, mailAttachments);
//                saveAttachment(mailAttachments);
            }
            mailUtil.close();
            System.out.println("解析邮件共耗时：" + (System.currentTimeMillis() - startTime) + "毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
